/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package point.of.sale.app;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;


/**
 *
 * @author card3r
 */
public class t_Model implements TableModel{
    ResultSet x =null;
    Object[][] continut_tabel = null;
    Vector<String> nume_coloane =null;
    int nrCol =0;
    int nrLin = 0;
    public void refresh_model(String qry)
    {
    try {
            nrCol = 0;
            nrLin = 0;
            SQLHandler k = new SQLHandler();
            //x = k.genericSelect("SELECT id_comanda,nume,prenume,telefon FROM clienti, comanda WHERE clienti.id_client = comanda.id_client");
            x = k.genericSelect(qry);
            nrCol = x.getMetaData().getColumnCount();
            nume_coloane = new Vector(nrCol);
            nume_coloane.clear();
            for(int i=0;i<nrCol;i++)
            {
                nume_coloane.add(x.getMetaData().getColumnName(i+1).toString());
            }
            while (x.next())
            {
                nrLin++;
            }
            continut_tabel=null;
            continut_tabel = new Object[nrLin+1][nrCol+1];
            x.beforeFirst();
            int s= 0;
            while(x.next())
            {

                    for(int t = 0;t<this.nrCol;t++)
                    {
                        continut_tabel[s][t] = x.getString(this.nume_coloane.elementAt(t).toString());
                    }
                s++;
            }

        } catch (SQLException ex) {
            Logger.getLogger(t_Model.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, ex.toString());
        }
    }
    public t_Model(String qry)
    {
        this.refresh_model(qry);
    }
    public int getRowCount() {
        return this.nrLin;
    }

    public int getColumnCount() {
        return this.nrCol;
    }

    public Object getValueAt(int i, int i1) {
        return this.continut_tabel[i][i1];
    }


    public String getColumnName(int i) {
        return nume_coloane.elementAt(i);
    }

    public Class<?> getColumnClass(int i) {
        return java.lang.String.class;
    }

    public boolean isCellEditable(int i, int i1) {
       // if ((i>=0) && (i1>=0))return true;
         return false;
    }

    public void setValueAt(Object o, int i, int i1) {
        //this.continut_tabel[i][i1] = o;
    }

    public void addTableModelListener(TableModelListener tl) {
        
        
    }

    public void removeTableModelListener(TableModelListener tl) {
        //this.removeTableModelListener(tl);
    }

}
